?><?
class cls_discussion extends cls_entity{

  function entity(){return "discussion";}

  function item($id){return parent::get_db_item(cls_discussion::entity(),$id);}

  function get($object,$page=0,$in_page=100,$order="desc"){return parent::get_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,$object->entity()),array(otAND,"entity_id",otEQ,$object->id()),array(otAND,"discussion_archived_rcpt",otEQ,0),array(otAND,"discussion_visible",otEQ,1)),array("discussion_readed"=>"asc","discussion_created"=>$order),array($page*$in_page=>$in_page));}
  function count($object){return parent::count_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,$object->entity()),array(otAND,"entity_id",otEQ,$object->id()),array(otAND,"discussion_archived_rcpt",otEQ,0),array(otAND,"discussion_visible",otEQ,1)),array("discussion_created"=>"desc"));}

  ///
  function get_hidden($object){return parent::get_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,$object->entity()),array(otAND,"entity_id",otEQ,$object->id()),array(otAND,"discussion_visible",otEQ,0)),array("discussion_readed"=>"asc","discussion_created"=>"desc"));}


  function count_unreaded($object){return parent::count_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,$object->entity()),array(otAND,"entity_id",otEQ,$object->id()),array(otAND,"discussion_readed",otEQ,0)));}
  function get_top_unreaded($object,$max=5){return parent::get_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,$object->entity()),array(otAND,"entity_id",otEQ,$object->id()),array(otAND,"discussion_readed",otEQ,0)),array("discussion_created"=>"desc"),array(0=>$max));}

  function archived_sent($object,$page=0,$in_page=100){return parent::get_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,"user"),array(otAND,"creator_id",otEQ,$object->id()),array(otAND,"discussion_archived_sent",otEQ,1)),array("discussion_created"=>"desc"),array($page*$in_page=>$in_page));}
  function archived_rcpt($object,$page=0,$in_page=100){return parent::get_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,"user"),array(otAND,"user_id",otEQ,$object->id()),array(otAND,"discussion_archived_rcpt",otEQ,1)),array("discussion_created"=>"desc"),array($page*$in_page=>$in_page));}

  function count_archived_sent($object){return parent::count_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,"user"),array(otAND,"creator_id",otEQ,$object->id()),array(otAND,"discussion_archived_sent",otEQ,1)));}
  function count_archived_rcpt($object){return parent::count_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,"user"),array(otAND,"user_id",otEQ,$object->id()),array(otAND,"discussion_archived_rcpt",otEQ,1)));}

  function count_archived($object){return parent::count_db_items(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,"user"),array(otAND,"creator_id",otEQ,$object->id()),array(otAND,array(array(otEMP,"discussion_archived_rcpt",otEQ,1),array(otOR,"discussion_archived_sent",otEQ,1)))));}


  function insert($object,$data){
    $data["entity_id"]=$object->id();
    $data["entity_name"]=$object->entity();
    return parent::insert(cls_discussion::entity(),$data);
  }

  function get_last($object){return parent::get_db_item(cls_discussion::entity(),array(array(otEMP,"entity_name",otEQ,$object->entity()),array(otAND,"entity_id",otEQ,$object->id())),array("discussion_created"=>"desc"),array(0=>1));}

  function delete($item){parent::delete(cls_discussion::entity(),array(array(otEMP,"discussion_id",otEQ,$item->id)));}

  function search($object,$string,$selector,$page=0){
  	return parent::get_db_items(
	  	cls_discussion::entity(),
  		array(
  			array(otEMP,"entity_name",otEQ,"user"),
  			array(otAND,"user_id",otEQ,$object->id()),
  			array(otAND,"discussion_archived_rcpt",otEQ,1)
  		),
  		array("discussion_created"=>"desc")
  	);
  }

  function search_string($object,$text,$rpct,$sent,$page=0,$in_page=100){
  	return parent::get_db_items(cls_discussion::entity(),
  			array(
 				  array(otEMP,
 				  	array(
  						array(otEMP,"discussion_text",otLIKE,$keyword),
  						array(otOR,"discussion_title",otLIKE,$keyword),
  					)
  				),
  			array(otAND,"entity_name",otEQ,$object->entity()),
  			array(otAND,"entity_id",otEQ,$object->id())),

  			array("discussion_created"=>"desc"),

  			array($page*$in_page=>$in_page)
  	);
  }
}

<#Include File="discussion/cls_discussion_item.php"            #>